package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexSearchVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    //上架接口
    @GetMapping(value = "/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable (value = "albumId") Long albumId)  {
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    @GetMapping(value = "/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable(value = "albumId") Long albumId) {
        searchService.lowerAlbum(albumId) ;
        return Result.ok() ;
    }

    @Operation(summary = "批量上架")
    @GetMapping("/batchUpperAblum")
    public Result upperAlbum()  {
        for (long i = 2; i <= 1500; i++) {   // 专辑id为1数据补全，缺少专辑属性数据
            searchService.upperAlbum(i);
        }
        return Result.ok();
    }

    // /api/search/albumInfo
    //专辑列表查询  实现效果 搜索框输入关键字，按回车，搜索结果
    @PostMapping
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery albumIndexQuery) throws IOException {
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery) ;
        return Result.ok(albumSearchResponseVo) ;
    }


    //根据一级分类id从es索引库查询专辑数据
    @GetMapping(value = "/channel/{category1Id}")
    public Result<List<AlbumInfoIndexSearchVo>> channel(@PathVariable(value = "category1Id") Long category1Id) throws IOException {
        List<AlbumInfoIndexSearchVo> albumInfoIndexSearchVos =  searchService.channel(category1Id) ;
        return Result.ok(albumInfoIndexSearchVos) ;
    }

    //关键字的补充
    @GetMapping(value = "/completeSuggest/{keyword}")
    public  Result<List<String>> completeSuggest(@PathVariable(value = "keyword") String keyword){
        List<String> list =searchService.completeSuggest(keyword);
        return Result.ok(list);
    }


    /**
     * 更新排行榜接口
     * 后期关于统计结果的更新需要通过定时任务进行自动更新
     */
    @GetMapping(value = "/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        searchService.updateLatelyAlbumRanking() ;
        return Result.ok() ;
    }


    //获取排行榜列表
    @GetMapping(value = "/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndex>> findRankingList(@PathVariable(value = "category1Id") Long category1Id , @PathVariable(value = "dimension") String dimension) {
        List<AlbumInfoIndex> albumInfoIndexList = searchService.findRankingList(category1Id , dimension) ;
        return Result.ok(albumInfoIndexList) ;
    }




}

